package com.boss.salary.workflow.service.impl;

import com.boss.common.entity.WfActionDTO;
import com.boss.common.service.IWorkflowBusiness;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.dao.IAgencyVerifyDao;
import com.boss.salary.entity.AgencyVerify;
import com.boss.salary.workflow.service.ISalaryWorkFlowService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SalaryWorkFlowServiceImpl implements ISalaryWorkFlowService {

    /**
     * 工资流程定义key
     */
        private static final String SALARY_PROCESS_DEF_KEY = "salary_agency_verify";

    /**
     * 工資多批次
     * 工资流程定义key
     */
    private static final String SALARY_PROCESS_DOUBLE = "salary_month_end_double";

    /**
     * 工作流实现类
     */
    @Autowired
    private IWorkflowBusiness workflowBusiness;

    @Autowired
    private IAgencyVerifyDao iAgencyVerifyDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlowList(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        workflowBusiness.doWorkFlowList(userDTO, wfActionDTO);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlowList1(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        workflowBusiness.doWorkFlowList(userDTO, wfActionDTO);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlow(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        workflowBusiness.doWorkFlow(userDTO, wfActionDTO);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlowMultiple(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        workflowBusiness.doWorkFlow(userDTO, wfActionDTO);
    }

    @Override
    public List<String> queryWorkflowInfos(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        WfActionDTO wfActionDTO1=new WfActionDTO();
        BeanUtils.copyProperties(wfActionDTO,wfActionDTO1);
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO1.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        List<String> list = workflowBusiness.queryWorkflowInfo(userDTO, wfActionDTO);
        List<String> list1 = workflowBusiness.queryWorkflowInfo1(userDTO, wfActionDTO1);
        if(null!=list1&&list1.size()>0){
            if(null!=list&&list1.size()>0){
                list.addAll(list1);
            }else {
                return list1;
            }
        }
        return list;
    }

    @Override
    public List<String> queryWorkflowInfo(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        WfActionDTO wfActionDTO1=new WfActionDTO();
        BeanUtils.copyProperties(wfActionDTO,wfActionDTO1);
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        wfActionDTO1.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        List<String> list = workflowBusiness.queryWorkflowInfo(userDTO, wfActionDTO);
        List<String> list1 = workflowBusiness.queryWorkflowInfo1(userDTO, wfActionDTO1);
        if(null!=list1&&list1.size()>0){
            if(null!=list&&list1.size()>0){
                list.addAll(list1);
            }else {
                return list1;
            }
        }
        return list;
    }


    @Override
    public List<String> queryWorkflowInfoMultiple(UserDTO userDTO, WfActionDTO wfActionDTO) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        List<String> list = workflowBusiness.queryWorkflowInfo(userDTO, wfActionDTO);
        return list;
    }

    @Override
    public List<Map> queryWorkflowInfoStatus(UserDTO userDTO, WfActionDTO wfActionDTO,List<String> statuses,List<String> bizkeyList) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        List<Map> list = workflowBusiness.queryWorkFlowStatus(userDTO, wfActionDTO,statuses,bizkeyList);
        return list;
    }
    @Override
    public List<Map> queryWorkflowInfoDouble(UserDTO userDTO, WfActionDTO wfActionDTO,List<String> statuses,List<String> bizkeyList) throws AppRuntimeException {
        wfActionDTO.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        List<Map> list = workflowBusiness.queryWorkFlowStatus(userDTO, wfActionDTO,statuses,bizkeyList);
        return list;
    }
    @Override
    public List<Map> getAuditInstruction(UserDTO userDTO, WfActionDTO wfActionDTO,String agencyId) {
        List<Map> list =new ArrayList<>();
        //AgencyVerify agencyVerify = iAgencyVerifyDao.getInfoByAgencyId(agencyId);
        List<AgencyVerify> agencyVerifyList = iAgencyVerifyDao.getInfoByAgency(agencyId);
        AgencyVerify agencyVerify1=new AgencyVerify();
        AgencyVerify agencyDouble=new AgencyVerify();
       for(AgencyVerify agencyVerify:agencyVerifyList){
           if(agencyVerify.getIsThirteen()>0){
               BeanUtils.copyProperties(agencyVerify,agencyDouble);
           }else if(agencyVerify.getIsThirteen()<=0){
               BeanUtils.copyProperties(agencyVerify,agencyVerify1);
           }
       }
       if(!StringUtils.isEmpty(agencyVerify1)) {
           String bizKey = agencyVerify1.getBizKey();
           wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
           wfActionDTO.setBusinessKeys(bizKey);
           wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
           wfActionDTO.setProvince(userDTO.getProvince());
           List<Map> maps = workflowBusiness.getAuditInstruction(wfActionDTO);
           if(maps!=null&&maps.size()>0){
               String content="";
               String timestamp="";
               String color="";
               for (Map map : maps) {
                   Map newMap =new HashMap();
                   if("null".equals(String.valueOf(map.get("actionUser")))){
                       content="当前节点："+String.valueOf(map.get("nodeName"))+"；"+"操作人： ；"
                               + "状态：" +String.valueOf(map.get("actionName"))+"；"+ "意见："+ String.valueOf(map.get("message"));
                       timestamp=String.valueOf(map.get("actionTime"));
                       if("退回".equals(String.valueOf(map.get("actionName")))){
                           //红色
                           color="#FF0000";
                       }else{
                           //绿色
                           color="#00FF00";
                       }
                   }else{
                       content="节点："+String.valueOf(map.get("nodeName"))+"；"+"操作人："+String.valueOf(map.get("actionUser"))+"；"
                               + "状态：" +String.valueOf(map.get("actionName"))+"；"+ "意见："+ String.valueOf(map.get("message"));
                       timestamp=String.valueOf(map.get("actionTime"));
                       if("退回".equals(String.valueOf(map.get("actionName")))){
                           //红色
                           color="#FF0000";
                       }else{
                           //绿色
                           color="#00FF00";
                       }
                   }

                   newMap.put("content",content);
                   newMap.put("timestamp",timestamp);
                   newMap.put("color",color);
                   list.add(newMap);
               }
           }

       }
        if(!StringUtils.isEmpty(agencyDouble)) {
            String bizKey = agencyDouble.getBizKey();
            wfActionDTO.setProcessDefKey(SALARY_PROCESS_DOUBLE);
            wfActionDTO.setBusinessKeys(bizKey);
            wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
            wfActionDTO.setProvince(userDTO.getProvince());
            List<Map> maps = workflowBusiness.getAuditInstruction(wfActionDTO);
            if(maps!=null&&maps.size()>0){
                String content="";
                String timestamp="";
                String color="";
                for (Map map : maps) {
                    Map newMap =new HashMap();
                    if("null".equals(String.valueOf(map.get("actionUser")))){
                        content="当前节点："+String.valueOf(map.get("nodeName"))+"；"+"操作人： ；"
                                + "状态：" +String.valueOf(map.get("actionName"))+"；"+ "意见："+ String.valueOf(map.get("message"));
                        timestamp=String.valueOf(map.get("actionTime"));
                        if("退回".equals(String.valueOf(map.get("actionName")))){
                            //红色
                            color="#FF0000";
                        }else{
                            //绿色
                            color="#00FF00";
                        }
                    }else{
                        content="节点："+String.valueOf(map.get("nodeName"))+"；"+"操作人："+String.valueOf(map.get("actionUser"))+"；"
                                + "状态：" +String.valueOf(map.get("actionName"))+"；"+ "意见："+ String.valueOf(map.get("message"));
                        timestamp=String.valueOf(map.get("actionTime"));
                        if("退回".equals(String.valueOf(map.get("actionName")))){
                            //红色
                            color="#FF0000";
                        }else{
                            //绿色
                            color="#00FF00";
                        }
                    }

                    newMap.put("content",content);
                    newMap.put("timestamp",timestamp);
                    newMap.put("color",color);
                    list.add(newMap);
                }
            }

        }
        return list;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSalaryFlowLists(UserDTO userDTO, WfActionDTO wfActionDTO, String getAgencyIds) throws AppRuntimeException {
        WfActionDTO wfActionDTO1=new WfActionDTO();
        BeanUtils.copyProperties(wfActionDTO,wfActionDTO1);
        List<AgencyVerify> agencyVerifyList = iAgencyVerifyDao.agencyVerifyByAgencyId(getAgencyIds);
        for(AgencyVerify agencyVerify:agencyVerifyList){
        if(agencyVerify.getIsThirteen().equals(0)){
            wfActionDTO.setProcessDefKey(SALARY_PROCESS_DEF_KEY);
        }else {
            wfActionDTO1.setProcessDefKey(SALARY_PROCESS_DOUBLE);
        }
        }
        wfActionDTO.setUserId(userDTO.getGuid());
        wfActionDTO.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO.setProvince(userDTO.getProvince());
        wfActionDTO1.setUserId(userDTO.getGuid());
        wfActionDTO1.setYear(String.valueOf(userDTO.getYear()));
        wfActionDTO1.setProvince(userDTO.getProvince());

        if(!StringUtils.isEmpty(wfActionDTO.getProcessDefKey())){
            workflowBusiness.doWorkFlow(userDTO, wfActionDTO);
        }
        if(!StringUtils.isEmpty(wfActionDTO1.getProcessDefKey())){
            workflowBusiness.doWorkFlow(userDTO, wfActionDTO1);
        }
    }
}
